package cn.yuyiling.jelly.sr.service.impl;


import cn.yuyiling.jelly.kg.api.KnowledgePointService;
import cn.yuyiling.jelly.sr.api.KnowledgeProgressService;
import cn.yuyiling.jelly.sr.mongodb.entity.KnowledgePointProgress;
import cn.yuyiling.jelly.sr.mongodb.repository.KnowledgePointProgressRepository;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@DubboService
public class KnowledgeProgressServiceImpl implements KnowledgeProgressService {

    private static final Logger logger = LoggerFactory.getLogger(KnowledgeProgressServiceImpl.class);

    @Autowired
    private KnowledgePointProgressRepository knowledgePointProgressRepository;

    @DubboReference
    private KnowledgePointService knowledgePointService;


    @Override
    public Map<String, String> getKnowledgeHeatmapColors(String userId) {
        List<KnowledgePointProgress> progressList = knowledgePointProgressRepository.findByUserId(userId);
        Map<String, String> heatmap = new HashMap<>();
        for (KnowledgePointProgress progress : progressList) {
            String color;
            if (progress.getCompletionRate() >= 80) {
                color = "green"; // 绿色（100%-80%）
            } else if (progress.getCompletionRate() >= 60) {
                color = "yellow"; // 黄色（79%-60%）
            } else {
                color = "red"; // 红色（59%-0%）
            }
            String knowledgePointId = progress.getKnowledgePointId();
            String knowledgePointName = knowledgePointService.getKnowledgePointById(knowledgePointId).getName();
            heatmap.put(knowledgePointName, color);
        }
        return heatmap;
    }

    @Override
    public List<KnowledgePointProgress> findByUserId(String userId) {
        return knowledgePointProgressRepository.findByUserId(userId);
    }
}